﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options;
using System.Buffers;

namespace Zeus.Mvc.Options
{
    using InputFormatters;
    public class ZeusMvcOptionSetup : IConfigureOptions<MvcOptions>
    {
        private readonly ArrayPool<char> _charArrayPool;
        private readonly ObjectPoolProvider _objectPoolProvider;
        private ZeusMvcOptions _zeusMvcOptions;
        private readonly ILoggerFactory _loggerFactory;


        public ZeusMvcOptionSetup(ArrayPool<char> arrayPool, ObjectPoolProvider objectPoolProvider, ZeusMvcOptions zeusMvcOptions, ILoggerFactory loggerFactory)
        {
            _charArrayPool = arrayPool;
            _objectPoolProvider = objectPoolProvider;
            _zeusMvcOptions = zeusMvcOptions;
            _loggerFactory = loggerFactory;
        }

        public void Configure(MvcOptions options)
        {
            options.InputFormatters.Add(new ZeusInputFormatter(
                _charArrayPool,
                _objectPoolProvider,
                _zeusMvcOptions.JsonSerializerSettings,
                _loggerFactory.CreateLogger<ZeusInputFormatter>()));
        }
    }
}
